package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysRoleDTO;
import com.andy.blog.admin.dto.SysUserDTO;
import com.andy.blog.admin.service.SysRoleBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.core.domain.entity.SysRole;
import com.andy.blog.common.core.domain.entity.SysUser;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.domain.SysRoleDO;
import com.andy.blog.dal.service.SysRoleDalService;
import com.andy.blog.dal.service.SysUserDalService;
import com.andy.blog.dal.vo.SysRoleVO;
import com.andy.blog.dal.vo.SysUserRoleVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {
    @Autowired
    private SysRoleDalService roleService;

    @Autowired
    private SysUserDalService userService;

    @Resource
    private SysRoleBusService sysRoleBusService;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SysRoleDTO>> listPage(SysRoleVO sysRoleVO) {
        return GeneralResponseResult.success(sysRoleBusService.listPage(sysRoleVO));
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public GeneralResponseResult<SysRoleDTO> getInfo(@PathVariable Long roleId) {
        return GeneralResponseResult.success(sysRoleBusService.getInfo(roleId));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public GeneralResponseResult<Void> add(@Validated @RequestBody SysRoleVO sysRoleVO) {
        sysRoleBusService.insertRole(sysRoleVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<Void> edit(@Validated @RequestBody SysRoleVO sysRoleVO) {
        sysRoleBusService.edit(sysRoleVO);
        return GeneralResponseResult.success();
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/changeStatus")
    public GeneralResponseResult<Void> changeStatus(@RequestBody SysRoleVO sysRoleVO) {
        sysRoleBusService.changeStatus(sysRoleVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{roleId}")
    public GeneralResponseResult<Void> remove(@PathVariable Long roleId) {
        sysRoleBusService.deleteRoleById(roleId);
        return GeneralResponseResult.success();
    }

    /**
     * 获取角色选择框列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping("/optionselect")
    public GeneralResponseResult<List<SysRoleDO>> optionselect() {
        return GeneralResponseResult.success(roleService.selectRoleAll());
    }

    /**
     * 查询已分配用户角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    public GeneralResponseResult<List<SysUserDTO>> allocatedList(SysRoleVO sysRoleVO) {
        return GeneralResponseResult.success(sysRoleBusService.selectAllocatedList(sysRoleVO));
    }

    /**
     * 查询未分配用户角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/authUser/unallocatedList")
    public GeneralResponseResult<PageUtilsDTO<SysUser>> unallocatedList(SysUser user) {
        List<SysUser> list = userService.selectUnallocatedList(user);
        return GeneralResponseResult.success(PageUtilsDTO.build(new PageInfo<>(list).getTotal(), list));
    }

    /**
     * 取消授权用户
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authUser/cancel")
    public GeneralResponseResult<Void> cancelAuthUser(@RequestBody SysUserRoleVO sysUserRoleVO) {
        sysRoleBusService.deleteAuthUser(sysUserRoleVO);
        return GeneralResponseResult.success();
    }

    /**
     * 批量选择用户授权
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authUser/selectAll")
    public GeneralResponseResult<Void> selectAuthUserAll(Long roleId, Long[] userIds) {
        return roleService.insertAuthUsers(roleId, userIds) > 0 ? GeneralResponseResult.success() : GeneralResponseResult.error();
    }
}
